﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Xml;
using System.Net;

public partial class SLogin : INTPage
{
    //域用户登录
    [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
    public static extern int LogonUser(String lpszUserName, String lpszDomain, 
        String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
    public const int LOGON32_LOGON_INTERACTIVE = 3;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    Login login = new Login();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Language"] = "zh-cn";
            Session["UserCode"] = null;
            Session["UserName"] = null;
            Session["LoginName"] = null;
            bindComany();
        }
    }
    //绑定公司
    public void bindComany()
    {
        ddlCompany.DataSource = login.GetCompany();
        ddlCompany.DataValueField = "CoCode";
        ddlCompany.DataTextField = "CoName";
        ddlCompany.DataBind();
        this.ddlCompany.Items.Add(new ListItem("请选择 (Please Select)", "0"));
        //ddlCompany.SelectedIndex = 1;
        ddlCompany.Focus();
    }
    protected void cmdLogin_ServerClick(object sender, EventArgs e)
    {
        sysUserLogin(); 
    }

    // 系统用户登录
    protected void sysUserLogin()
    {
        try
        {
            if (txtUserName.Text.Trim().ToString() == "")
            {
                CommonMethod.regAlertMessage("请输入用户名！", this.Page);
                txtUserName.Focus();
            }
            else if (txtUserPwd.Text.Trim().ToString() == "")
            {
                CommonMethod.regAlertMessage("请输入密码！", this.Page);
                txtUserPwd.Focus();
            }
            else
            {
                IntPtr token = IntPtr.Zero;
                //判断用户输入的域名
                string domain = txtUserName.Text;                
                if (domain.Contains("\\"))
                {
                    #region 域登录
                    string[] domain1 = domain.Split('\\');
                    string domainUser = string.Empty;
                    string domainName = domain1[0].ToString();
                    if (domain1.Length > 1)
                    {
                        domainUser = domain1[1].ToString();
                    }
                    //判断用户输入域名为邮箱格式
                    string[] domainEmail = domain.Split('@');
                    string domainUserEmail = domainEmail[0].ToString();
                    string domainNameEmail = string.Empty;
                    if (domainEmail.Length > 1)
                    {
                        domainNameEmail = domainEmail[1].ToString();
                    }
                    //判断用户本身所在的域名
                    string domain2 = User.Identity.Name;
                    string[] domain11 = domain2.Split('\\');
                    string domainUser1 = string.Empty;
                    string domainName1 = domain11[0].ToString();
                    if (domain11.Length > 1)
                    {
                        domainUser1 = domain11[1].ToString();
                    }
                    if (LogonUser(domainUser, domainName, txtUserPwd.Text, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                    {
                        DataTable dt = login.userLogin(domainUser);
                        CommonMethod.regAlertMessage("success login in domain!", this);
                        if (dt.Rows.Count > 0)
                        {
                            setSession(dt);
                            Response.Redirect("SMain.aspx");
                        }
                    }
                    else
                    {
                        txtUserName.Text = domain;
                        CommonMethod.regAlertMessage("域用户信息不存在或已停用,请重新输入!", this);
                    }
                    #endregion
                }
                else
                {
                    #region OTS系统账号登陆
                    DataTable dt = login.userLogin(txtUserName.Text.Trim().ToString(), Login.EncryptPassword(txtUserPwd.Text.Trim(), Login.PasswordType.MD5));//,ddlCompany.SelectedValue.ToString()
                    if (dt.Rows.Count > 0)
                    {
                        setSession(dt);
                        Response.Redirect("SMain.aspx");//SMain.htm
                    }
                    else
                    {
                        txtUserName.Focus();
                        //CommonMethod.regAlertMessage(Login.EncryptPassword(txtUserPwd.Text.Trim(), Login.PasswordType.MD5), this.Page);
                        CommonMethod.regAlertMessage("用户信息不存在或已停用,请重新输入！", this.Page);
                    }
                    #endregion
                }
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("登录失败,请联系管理员!", this);
        }
    }
    
    // session设置
    protected void setSession(DataTable dt)
    {
        Session["LoginName"] = txtUserName.Text.Trim();
        Session["UserName"] = dt.Rows[0]["UserFullName"].ToString();
        Session["UserCode"] = dt.Rows[0]["UserCode"].ToString();
        Session["CoCode"] = ddlCompany.SelectedValue.ToString();
    }
}
